'''
https://leetcode.cn/problems/balanced-binary-tree/
'''
from typing import Optional

from c06_tree.utils import TreeNode


# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isValidBST(self, root: Optional[TreeNode]) -> bool:
        def process(root) -> (bool, int, int):
            if root is None:
                return (True, float('inf'), -float('inf'))
            left_info = process(root.left)
            right_info = process(root.right)
            flag = left_info[0] and right_info[0] and left_info[2] < root.val < right_info[1]
            min_value = min(left_info[1], right_info[1], root.val)
            max_value = max(left_info[2], right_info[2], root.val)
            return (flag, min_value, max_value)
        return process(root)[0]

root = TreeNode(1)
right = TreeNode(1)
root.right = right
res = Solution().isValidBST(root)
print(res)
